package com.magold.logic.polygon;

import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.magold.circuit.ICircuit;
import com.magold.circuit.ICircuitPath;
import com.magold.context.IContext;
import com.magold.physics.magnetic.BaseMagneticAffectable;
import com.magold.physics.magnetic.DirectionSolver;
import com.magold.physics.magnetic.MagneticPole;
import com.magold.physics.magnetic.Metal;
import com.magold.skill.cast.AttacherManager;

public class MetalPrimitive extends PrimitiveLogic implements Metal, DirectionSolver {
	private static Constants sConstants;
	private static float METAL_INFORM_DISTANCE = 150;
	private static FixtureDef sFixtureDef = PhysicsFactory.createFixtureDef(20.0f, 0.35f, 0.25f);
	public static void customize(Constants pConstants) {
		sConstants = pConstants;
	}

	private BaseMagneticAffectable mDelegate;
	public MetalPrimitive(PrimitiveShape pShape, BodyType pType) {
		super(pShape, sConstants.mRegion.deepCopy(), pType);
	}
	
	@Override
	public void createPhysicsData(PhysicsWorld pWorld) {
		super.createPhysicsData(pWorld);
		mDelegate = new BaseMagneticAffectable(getBody(), this);
		
		AttacherManager am = sConstants.mContext.getAttacherManager();
		float[] sceneCenter = mShape.getSceneCenterCoordinates();
		am.createAttacher(sceneCenter[0], sceneCenter[1], METAL_INFORM_DISTANCE, mShape.mConnector);
	}

	@Override
	protected FixtureDef getFixtureDef() {
		return sFixtureDef;
	}

	@Override
	public void onAffected(MagneticPole pPole, float pX, float pY, float pForce) {
		mDelegate.onAffected(pPole, pX, pY, pForce);
	}

	@Override
	public boolean isConnected() {
		return true;
	}

	private Vector2 mDirection = new Vector2();
	@Override
	public Vector2 computeDirection(MagneticPole pPole, float pX, float pY) {
		float[] center = mShape.getSceneCenterCoordinates();
		mDirection.set(pX-center[0], pY-center[1]);
		
		return mDirection.nor();
	}

	@Override
	public float[] getMagneticAffectPosition() {
		return mDelegate.getMagneticAffectPosition();
	}

	@Override
	protected IContext getContext() {
		return sConstants.mContext;
	}

	@Override
	public void onCurrentBegin() {
	}

	@Override
	public void onCurrentEnd() {
	}

	@Override
	public void onPathJoined(ICircuit pCircuit, ICircuitPath pPath) {
	}

	private int mID;
	@Override
	public void setID(int pID) {
		mID = pID;
	}

	@Override
	public int getID() {
		return mID;
	}

	@Override
	public float[] getWireInPosition() {
		return mShape.convertLocalToSceneCoordinates(mShape.getWidth()/2, mShape.getHeight()/2);
	}

	@Override
	public float[] getWireOutPosition() {
		return mShape.convertLocalToSceneCoordinates(mShape.getWidth()/2, mShape.getHeight()/2);
	}
}
